package com.bigfreakindeals.Activities;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigfreakindeals.R;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Controller.UIController;

public class BrowserActivity extends BaseActivity {

	ProgressBar progressBar;

	WebView appView;
	String startUrl;
	byte type = 1;

	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getActivityView();

	}

	public void getActivityView() {

		// Retrieve the uri to go to from intent
		startUrl = getIntent().getDataString();
		type = getIntent().getByteExtra(Constant.TAG_BROWSERTYPE, (byte)0);
		
		Log.i("BROWSERACTIVITY", startUrl+" "+type);
		setContentView(R.layout.activitybrowser);

		WebViewClient webViewClientExt = new WebViewClientExt();

		if (type == Constant.type_FORGETPASS) {
			((RelativeLayout) findViewById(R.id.contactus)).setVisibility(View.GONE);
			((TextView) findViewById(R.id.header_title)).setText("Forgot Password");
		} else if (type == Constant.type_FAQ) {
			((RelativeLayout) findViewById(R.id.contactus)).setVisibility(View.GONE);
			
			((TextView) findViewById(R.id.header_title)).setText("Fine Print and Deal FAQ");
			
		}else if (type == Constant.type_DEALFAQ) {
			((RelativeLayout) findViewById(R.id.contactus)).setVisibility(View.GONE);
			
			((TextView) findViewById(R.id.header_title)).setText("Fine Print and Deal FAQ");
		}else if (type == Constant.type_TandC) {
			
			((RelativeLayout) findViewById(R.id.contactus)).setVisibility(View.GONE);
			((TextView) findViewById(R.id.header_title)).setText("Terms and Conditions");
		
		}else if (type == Constant.type_MAP) {
			((RelativeLayout) findViewById(R.id.contactus)).setVisibility(View.GONE);
			((TextView) findViewById(R.id.header_title)).setText("Map");
		}else if (type == Constant.type_PRIVACY) {
			((RelativeLayout) findViewById(R.id.contactus)).setVisibility(View.GONE);
			((TextView) findViewById(R.id.header_title)).setText("Privacy Policy");
		}else if (type == Constant.type_SUPPORT) {
			((RelativeLayout) findViewById(R.id.contactus)).setVisibility(View.VISIBLE);
			((RelativeLayout) findViewById(R.id.contactus)).setBackgroundDrawable(ApplicationEx.getInstance().gradientGray_TOP_BOTTOM_BUY);
			((TextView) findViewById(R.id.header_title)).setText("Contact Support");
			
		}

		appView = (WebView) findViewById(R.id.webview);
		appView.setBackgroundColor(0xffffff);
		//setBackgroundDrawable(ApplicationEx.getInstance().gradientGray_TOP_BOTTOM_BUY);
		
		appView.setVerticalScrollbarOverlay(true);
		appView.setFocusableInTouchMode(true);
		appView.setFocusable(true);
		appView.requestFocus(View.FOCUS_DOWN);

		WebSettings webSettings = appView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
		webSettings.setDomStorageEnabled(true);
		webSettings.setSaveFormData(false);
		webSettings.setSavePassword(false);
		webSettings.setLoadsImagesAutomatically(true);
		webSettings.setBlockNetworkImage(false);
		webSettings.setGeolocationEnabled(true);
		
		appView.setWebViewClient(webViewClientExt);
		
		progressBar = (ProgressBar) findViewById(R.id.progress);
		progressBar.bringToFront();
		
		this.registerReceiver(browserRelatedBroadcastReceiver, new IntentFilter(Constant.EVENTS_FAQ));	
		
		Log.i("TYPE", ""+type);
		
		if (type == Constant.type_MAP) {
			goToPage(startUrl);
		}else if (type == Constant.type_FORGETPASS) {
			goToPage(startUrl);
		}else if (type == Constant.type_FAQ) {
			
			if (ApplicationEx.getInstance().myProfile.getFAQ() != null){
				
				progressBar.bringToFront();
				progressBar.setVisibility(View.GONE);
				System.out.println("1: "+ApplicationEx.getInstance().myProfile.getFAQ());
				appView.loadData(ApplicationEx.getInstance().myProfile.getFAQ(), "text/html", "utf-8");
				
			}else {
				System.out.println("2:"+ApplicationEx.getInstance().myProfile.getFAQ());
				
				ApplicationEx.getInstance().controller.getFAQ();			
			}
			
		}else if (type == Constant.type_DEALFAQ) {
			if (ApplicationEx.getInstance().myProfile.getDEALSFAQ() != null){
				
				progressBar.bringToFront();
				progressBar.setVisibility(View.GONE);
				appView.loadData(ApplicationEx.getInstance().myProfile.getDEALSFAQ(), "text/html", "utf-8");
				
			}else 
				ApplicationEx.getInstance().controller.getFAQ();			
			
			
		}else if (type == Constant.type_TandC || type == Constant.type_PRIVACY) {
			System.out.println("t@C"+ApplicationEx.getInstance().myProfile.getTAndC());
			if (ApplicationEx.getInstance().myProfile.getTAndC() != null){
				progressBar.bringToFront();
				progressBar.setVisibility(View.GONE);
				
				appView.loadData(ApplicationEx.getInstance().myProfile.getTAndC(), "text/html", "utf-8");
				
			}else {
				ApplicationEx.getInstance().controller.getFAQ();		
			}
			
		}else if (type == Constant.type_SUPPORT) {
			System.out.println("t@C"+ApplicationEx.getInstance().myProfile.getDEALSFAQ());
			
			if (ApplicationEx.getInstance().myProfile.getDEALSFAQ() != null){
				
				progressBar.bringToFront();
				progressBar.setVisibility(View.GONE);
				appView.loadData(ApplicationEx.getInstance().myProfile.getDEALSFAQ(), "text/html", "utf-8");
				
			}else 
				ApplicationEx.getInstance().controller.getFAQ();			
			
			
		}
		
		
	}

	public BroadcastReceiver browserRelatedBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {

				if (intent.getAction().equals(Constant.EVENTS_FAQ)) {
					progressBar.bringToFront();
					progressBar.setVisibility(View.GONE);
					// Log.e("WAR", "going to page " + url);
					appView.loadData(ApplicationEx.getInstance().myProfile.getFAQ(), "text/html", "utf-8");
					}
			}
	};
	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(browserRelatedBroadcastReceiver);
	}
	@Override
	public void onResume() {
		super.onResume();
		
		

	}

	private void goToPage(String url) {
		progressBar.bringToFront();
		progressBar.setVisibility(View.VISIBLE);
		// Log.e("WAR", "going to page " + url);
		appView.loadUrl(url);
	}

	private class WebViewClientExt extends WebViewClient {
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			Log.e("BrowserActivity", "shouldOverrideUrlLoading"+type+" "+url);
			
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Log.e("BrowserActivity", "shouldOverrideUrlLoading");
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			if (progressBar != null) {
				progressBar.setVisibility(View.GONE);
			}
		}
	}
}
